//
//  TogetherLocationSelectorViewController.m
//  Together_iOS
//
//  Created by applex on 14-3-1.
//  Copyright (c) 2014年 QIU Shuang. All rights reserved.
//

#import "TogetherRegionSelectorViewController.h"
#import "AFNetworking.h"
#import "TogetherHttpClient.h"
#import "TogetherLocationSelectorViewController.h"
#import "TogetherUtils.h"

@interface TogetherRegionSelectorViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *regions;

@end

@implementation TogetherRegionSelectorViewController

- (id)init
{
    self = [super init];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return self;
}


- (void)loadView {
    [super loadView];
    _tableView.frame = self.view.bounds;
    [self.view addSubview:_tableView];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	[self refresh];
    self.title = @"选择位置";
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!self.regions) {
        return 0;
    }
    return self.regions.count;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"LocationSelectorCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSDictionary *dic = [self.regions objectAtIndex:indexPath.row];
    cell.textLabel.text = dic[@"district_name"];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [self.regions objectAtIndex:indexPath.row];
    TogetherLocationSelectorViewController *locationSelector = [[TogetherLocationSelectorViewController alloc] init];
    locationSelector.settingView = self.settingView;
    locationSelector.region = dic;
    [self.navigationController pushViewController:locationSelector animated:YES];
}


#pragma mark - Actions

- (void)refresh {
    static NSString *jsonString = @"[{\"district_name\":\"卢湾区\",\"neighborhoods\":[\"淮海路\",\"打浦桥\",\"新天地\",\"瑞金宾馆区\"]},{\"district_name\":\"徐汇区\",\"neighborhoods\":[\"徐家汇\",\"万体馆\",\"衡山路\",\"复兴西路/丁香花园\",\"肇嘉浜路沿线\",\"音乐学院\",\"龙华\",\"漕河泾/田林\",\"上海南站\"]},{\"district_name\":\"静安区\",\"neighborhoods\":[\"静安寺\",\"同乐坊\",\"南京西路\"]},{\"district_name\":\"长宁区\",\"neighborhoods\":[\"虹桥\",\"中山公园\",\"上海影城/新华路\",\"天山\",\"古北\",\"动物园/虹桥机场\",\"北新泾\"]},{\"district_name\":\"浦东新区\",\"neighborhoods\":[\"上南地区\",\"外高桥\",\"金桥\",\"源深体育中心\",\"陆家嘴\",\"八佰伴\",\"世纪公园\",\"张江\",\"塘桥\",\"川沙\",\"金杨地区\",\"碧云社区\",\"三林地区\",\"惠南镇\",\"康桥/周浦\",\"东明\",\"浦东机场\",\"临沂/南码头\",\"北蔡\"]},{\"district_name\":\"黄浦区\",\"neighborhoods\":[\"董家渡\",\"外滩\",\"老西门\",\"城隍庙\",\"南京东路\",\"人民广场\"]},{\"district_name\":\"普陀区\",\"neighborhoods\":[\"曹杨地区\",\"长风公园/华师大\",\"中山北路/甘泉地区\",\"梅川路\",\"曹家渡\",\"长寿路\",\"真如\",\"武宁地区\",\"月星环球港\",\"桃浦\"]},{\"district_name\":\"闸北区\",\"neighborhoods\":[\"彭浦新村\",\"大宁地区\",\"闸北公园\",\"火车站\",\"北区汽车站\",\"大悦城\"]},{\"district_name\":\"虹口区\",\"neighborhoods\":[\"虹口足球场\",\"曲阳地区\",\"海宁路/七浦路\",\"四川北路\",\"北外滩\",\"临平路/和平公园\",\"凉城/江湾镇\"]},{\"district_name\":\"杨浦区\",\"neighborhoods\":[\"控江地区\",\"五角场/大学区\",\"平凉路\",\"黄兴公园\",\"中原地区\",\"鞍山新村\"]},{\"district_name\":\"闵行区\",\"neighborhoods\":[\"春申地区\",\"南方商城\",\"莘庄\",\"七宝\",\"万源城/东兰路\",\"老闵行\",\"虹桥镇\",\"虹梅路\",\"龙柏地区\",\"浦江镇\"]},{\"district_name\":\"宝山区\",\"neighborhoods\":[\"大华地区\",\"庙行/共康\",\"淞滨地区\",\"上海大学\",\"杨行\",\"高境\",\"淞南\",\"月浦\",\"宝山城区\",\"通河/泗塘\",\"顾村公园\",\"美兰湖\"]},{\"district_name\":\"松江区\",\"neighborhoods\":[\"松江镇\",\"松江大学城\",\"佘山\",\"九亭\",\"泗泾\",\"新桥\"]},{\"district_name\":\"嘉定区\",\"neighborhoods\":[\"江桥\",\"南翔\",\"安亭\",\"嘉定镇\"]},{\"district_name\":\"青浦区\",\"neighborhoods\":[\"朱家角\",\"青浦镇\"]},{\"district_name\":\"金山区\",\"neighborhoods\":[\"金山卫\"]},{\"district_name\":\"奉贤区\",\"neighborhoods\":[\"南桥\",\"西渡\"]},{\"district_name\":\"崇明县\",\"neighborhoods\":[\"东滩/前哨农场\",\"南门\"]}]";
    
    NSArray *jsonArray = [TogetherUtils arrayOrNSDictionaryFromJsonString:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    self.regions = jsonArray;
    return;
    NSString *requestUrl = [[TogetherHttpClient serverAddress] stringByAppendingString:@"/cities"];
    requestUrl = @"http://api.dianping.com/v1/metadata/get_regions_with_businesses?appkey=9617810861&sign=F224F5D408359E0DDD7321418CF6A7FFA5266189";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:requestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        if ([dic[@"status"] isEqualToString:@"OK"]) {
            self.regions = [[dic[@"cities"] objectAtIndex:0] objectForKey:@"districts"];
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

}

@end
